#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Python version: 3.5.4
'''
@File    :   run.py
@Time    :   2020/04/08 11:33:57
@Author  :   Wu Xueru 
@Version :   1.0
@Contact :   t01051@163.com
@License :   GPL-2.0-only
@Desc    :   
'''

# here put the import lib
import cv2
from os import popen as os_popen
from sys import exit
from requests import get as requests_get

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}

Windows_name = 'If need help press left button'

mouse_wheel = 0
mouse_wheel_step = 5
def MouseEvent(event, x, y, flags, param):
    global mouse_wheel
    global mouse_wheel_step

    # 滚轮缩放图像
    if event == cv2.EVENT_MOUSEWHEEL:
        if flags > 0 and mouse_wheel < 99:
            mouse_wheel = mouse_wheel+mouse_wheel_step if mouse_wheel+mouse_wheel_step<=99 else 99
        elif flags < 0 and mouse_wheel_step > 0:
            mouse_wheel = mouse_wheel-mouse_wheel_step if mouse_wheel-mouse_wheel_step>=0 else 0
        requests_get(ipcamera_url+'/ptz?zoom='+str(mouse_wheel), headers)
        
    # 鼠标右键聚焦摄像头
    if event == cv2.EVENT_RBUTTONDOWN:
        requests_get(ipcamera_url+'/focus', headers)

    # 鼠标左键弹出帮助信息
    if event == cv2.EVENT_LBUTTONDOWN:
        os_popen('cscript message.vbs "help：在显示窗口中，鼠标滚轮缩放图像、右键自动对焦摄像头、左键显示帮助信息"')

if __name__ == '__main__':
    ipcamera_url = None
    with open('config.txt', 'r') as f:
        ipcamera_url = f.readline()
    print(ipcamera_url)

    try:
        res = requests_get(ipcamera_url+'/videostatus', headers).json()
        if res.get('result') != 'status':
            os_popen('cscript message.vbs "检查摄像头失败！"')
            exit()

        # 设置摄像头为后置摄像头 图像大小为640x480 横屏模式 流质量85% 
        requests_get(ipcamera_url+'/settings/ffc?set=off', headers)
        requests_get(ipcamera_url+'/settings/video_size?set=640x480', headers)
        requests_get(ipcamera_url+'/settings/photo_size?set=640x480', headers)
        requests_get(ipcamera_url+'/settings/orientation?set=landscape', headers)
        requests_get(ipcamera_url+'/settings/quality?set=95', headers)
        requests_get(ipcamera_url+'/ptz?zoom=0', headers)
    except:
        os_popen('cscript message.vbs "config.txt中摄像头地址错误或未在手机上运行软件！"')
        exit()

    cv2.namedWindow(Windows_name, cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
    cv2.setMouseCallback(Windows_name, MouseEvent)
    
    while True:
        # '/video' 模式会大量占用网络
        # img_src = cv2.VideoCapture(ipcamera_url+'/video')
        img_src = cv2.VideoCapture(ipcamera_url+'/shot.jpg')
        if img_src.isOpened():
            img = None
            ret,img = img_src.read()
            cv2.imshow(Windows_name, img)
  
        cv2.waitKey(5)

        if cv2.getWindowProperty(Windows_name, cv2.WND_PROP_AUTOSIZE) != 0.0:
            break
    
    try:
        img_src.release()
        cv2.destroyAllWindows()
    except:
        exit()
